package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.Address;
import com.cjq.allservice.entity.User;
import com.cjq.allservice.entity.vo.AddressQuery;
import com.cjq.allservice.entity.vo.UserQuery;
import com.cjq.allservice.service.AddressService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-01-31
 */
@RestController
@RequestMapping("/allservice/address")
@CrossOrigin
public class AddressController {

    private final AddressService addressService;

    public AddressController(AddressService addressService){
        this.addressService=addressService;
    }

    @ApiOperation("对地址进行禁用操作")
    @PostMapping("/updateStatusById")
    public R updateStatusById(@RequestParam String addressId){
        Address address = addressService.getById(addressId);
        if (address.getStatus()==1){
            address.setStatus(0);
        }else {
            address.setStatus(1);
        }
        boolean b = addressService.updateById(address);
        if (b==true){
            return R.succ().message("修改成功！");
        }else {
            return R.fail().message("修改失败！");
        }
    }

    @ApiOperation("根据用户id获取个人地址")
    @GetMapping("/getAddressByUserId")
    public R getAddressByUserId(@RequestParam String userId){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        List list = addressService.list(wrapper);
        return R.succ().data("addressList",list);
    }

    @GetMapping("/getAllAddress")
    public R getAllAddress(){
        List<Address> list = addressService.list();
        return R.succ().data("addressList",list);
    }

    @ApiOperation("根据地址id获取地址信息")
    @GetMapping("/getAddressInfoById")
    public R getAddressInfoById(@RequestParam String addressId){
        Address address = addressService.getById(addressId);
        return R.succ().data("addressInfo",address);
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping("/deleteById")
    public R deleteById(@RequestParam String addressId){
        boolean b = addressService.removeById(addressId);
        if (b==true){
            return R.succ().message("删除成功！");
        }else {
            return R.fail().message("删除失败！");
        }
    }

    @ApiOperation("根据id进行编辑地址")
    @PostMapping("/updateAddressById")
    public R updateAddressById(@RequestBody Address address){
        boolean b = addressService.updateById(address);
        if (b==true){
            return R.succ().message("修改成功！");
        }else {
            return R.fail().message("修改失败！");
        }
    }

    @ApiOperation("进行地址的添加")
    @PostMapping("/addAddress")
    public R addAddress(@RequestBody Address address){
        boolean b = addressService.save(address);
        if (b==true){
            return R.succ().message("添加成功！");
        }else {
            return R.fail().message("添加失败！");
        }
    }

    @ApiOperation("条件查询用户地址带分页")
    //条件查询带分页
    @PostMapping ("pageAddressCondition/{current}/{limit}")
    public R pageAddressCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) AddressQuery addressQuery){
        Page<Address> page=new Page<>(current,limit);
        QueryWrapper<Address> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = addressQuery.getAddressId();
        String name = addressQuery.getUserName();
        String phone=addressQuery.getPhone();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("address_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("username",name);
        }
        if (!StringUtils.isEmpty(phone)){
            wrapper.like("phone",phone);
        }
        wrapper.orderByDesc("create_time");

        addressService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<Address> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }


}

